<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2022-06-27
 * Time: 10:25:54
 * Info:
 */

namespace support\lib;

class Form
{

    /**
     * 富文本编辑器
     *
     * @param $name   name
     * @param $style  样式
     * @param $isload 是否加载js,当该页面加载过编辑器js后，无需重复加载
     */
    public static function editor($name = 'content', $val = '', $style = '', $isload = true): string
    {
        $val        = htmlspecialchars_decode($val);
        $editorType = get_config('site_editor');
        $res        = '';
        switch ($editorType) {
            case 'uEditorPlus';
                $res = self::editor_uEditorPlus($name, $val, $isload, $style);
                break;
            case 'iceEditor';
                $res = self::editor_iceEditor($name, $val, $isload, $style);
                break;
        }

        return $res;
    }

    /**
     * 百度编辑器
     *
     * @param $name
     * @param $val
     * @param $isLoad
     */
    private static function editor_uEditorPlus($name, $val, $isLoad, $style = '')
    {
        $toolbars = get_config('ueditor_icon');
        if ( ! empty($toolbars)) {
            $toolbars = array_filter(explode(',', $toolbars));
        }
        $toolbars1 = [];
        foreach ($toolbars as $key => $item) {
            if (is_int($key / 5) && $key > 0) {
                array_push($toolbars1, "|");
                array_push($toolbars1, $toolbars[$key]);
            } else {
                array_push($toolbars1, $toolbars[$key]);
            }
        }

        if ( ! empty($toolbars1)) {
            $toolbars2 = 'toolbars:['.json_encode($toolbars1).']';
        } else {
            $toolbars2 = "";
        }

        $configJs = DIRECTORY_SEPARATOR.'static'.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'ueditor-plus-2.2'.DIRECTORY_SEPARATOR.'ueditor.config.js';
        $Js2      = DIRECTORY_SEPARATOR.'static'.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'ueditor-plus-2.2'.DIRECTORY_SEPARATOR.'ueditor.all.js';
        $string   = '';
        $string   .= '<script id="container" name="'.$name.'" type="text/plain" style="'.$style.'" >'.$val.'</script>';
        $string   .= '<script type="text/javascript" src="'.$configJs.'"></script>';
        $string   .= '<script type="text/javascript" src="'.$Js2.'"></script>';
        $string   .= '<script type="text/javascript">';
        $string   .= 'var ue = UE.getEditor("container",{';
        $string   .= $toolbars2;
        $string   .= '});';
        $string   .= '</script>';

        return $string;

    }

    /**
     * iceEditor
     *
     * @param $name
     * @param $val
     * @param $isLoad
     */
    private static function editor_iceEditor($name, $val, $isLoad, $style = '')
    {
        $Codecss = DIRECTORY_SEPARATOR.'static'.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'iceEditor'.DIRECTORY_SEPARATOR.'iceCode.css';
        $libDir  = DIRECTORY_SEPARATOR.'static'.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'iceEditor'.DIRECTORY_SEPARATOR.'iceEditor.min.js?v=1.0';
        $string  = '';
        if ($isLoad) {
            $string .= '<link href="'.$Codecss.'" rel="stylesheet">';
            $string .= '<script type="text/javascript" charset="utf-8" src="'.$libDir.'"></script>';
        }
        $string .= '<textarea id="editor" name="'.$name.'">'.$val.'</textarea>';
        $string .= '<script>
                iceEditor = new ice.editor("editor");
                iceEditor.uploadUrl="/common/upload/upload?editor_type=iceEditor";
                iceEditor.create();
                </script>
                ';

        return $string;
    }

}
